<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>抱抱堂-摇摇乐</title>
    <meta name="renderer" content="webkit">
    <meta name="viewport"
          content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="{:C('IMG_PATH')}static/css/main.css?v=10">
</head>
<body class="mycard-bg">
<section class="gift card">
    <h1>验证手机</h1>
    <section class="gift_personal_info" style="padding-top:0;">
        <p><input name="phone_number" type="number" placeholder="手机号"/></p>
        <p class="captcha_box"><input name="captcha_number" type="number" placeholder="短信验证码"/>
            <button class="long-yellow-btn captcha" id="captcha">获取验证码</button>
        </p>
        <section class="gift_btn">
            <button class="long-yellow-btn" id="form_profile" style="max-width:100%;">提交信息</button>
        </section>
    </section>
</section>
</div>
<footer class="white"><img src="{:C('IMG_PATH')}static/img/logo_white.png"/></footer>
<script src="{:C('IMG_PATH')}static/js/zepto.min.js"></script>
<script>
    (function () {
        var checkForm = {
            captchaTime: 1,
            captchaBtn: function (status, btn) {
                var time = 180;

                if (status == 0) {
                    clearTimeout(captchaTime);
                    btn.innerHTML = "获取验证码";
                    $(btn).attr("st", "1");
                    return;
                }

                var countDown = function () {
                    if (time > 0 && $(btn).attr("st") == 0) {
                        if (status == 1) {
                            btn.innerHTML = time + "秒剩余";
                        }
                        time--;
                        captchaTime = setTimeout(countDown, 1000);
                    } else {
                        checkForm.captchaBtn(0, btn);
                    }
                    $(btn).removeAttr("disabled");
                }

                $(btn).attr("st", "0");
                $(btn).attr("disabled", true);
                countDown();
            },
        };
        $("#captcha").click(function () {
            checkForm.captchaBtn(1, $("#captcha")[0]);
            var phoneNumber = $("input[name='phone_number']").val();
            $.ajax({
                url: "{:U('Portal/OpenApi/getPhoneCaptcha')}",
                cache: false,
                type: "POST",
                dataType: "json",
                data: {
                    "phone_number": phoneNumber
                },
                success: function (msg, textStatus) {
                    if (msg.errcode == 0) {
                        if (msg.data.status == 0) {
                            alert('验证码下发成功');
                            return false;
                        }
                        if (msg.data.status == 4) {
                            alert('每天限制3条短信已经用完，有问题请联系客服');
                            checkForm.captchaBtn(0, $("#captcha")[0]);
                            return;
                        }
                        alert('验证码下发失败，请核对手机号');
                        checkForm.captchaBtn(0, $("#captcha")[0]);
                    } else {
                        alert('验证码下发失败，请稍后重试');
                    }
                },
                error: function () {
                    alert('验证码下发失败，请重试');
                }
            });
            return false;
        });
        $("#form_profile").click(function () {
            var phoneNumber = $("input[name='phone_number']").val();
            var captchaNumber = $("input[name='captcha_number']").val();
            $.ajax({
                url: "{:U('Portal/OpenApi/verifyPhoneCode')}",
                cache: false,
                type: "POST",
                dataType: "json",
                data: {
                    "phone_number": phoneNumber,
                    "captcha_number": captchaNumber
                },
                success: function (msg, textStatus) {
                    if (msg.errcode == 0) {
                        if (msg.data.status == 0) {
                            alert("手机号绑定成功");
                            source = "{$_GET['source']}";
                            if (source) {
                                window.location.href = source;
                            } else {
                                window.location.href = "{$ticket}";
                            }
                        } else if (msg.data.status == 1) {
                            alert('请填写完整表单信息');
                        } else {
                            alert('请核对验证码');
                        }
                        return false;
                    }
                },
                error: function () {
                    alert('请核对表单信息是否正确');
                }
            });
            return false;
        });
    })();
</script>
</body>
</html>
